<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="供应商" prop="supplyName">
        <el-input
          v-model="queryParams.supplyName"
          placeholder="请输入供应商"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createName">
        <el-input
          v-model="queryParams.createName"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="isCt">
        <el-select
          placeholder="请选择类型"
          v-model="queryParams.isCt"
          @keyup.enter.native="handleQuery"
        >
          <el-option label="采购单" value="1" />
          <el-option label="采退单" value="2" />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="purchaseList">
      <el-table-column type="index" label="序号" align="center" />
      <el-table-column label="单号" align="center" prop="purchaseCode" />
      <el-table-column label="供应商" align="center" prop="supplyName" />
      <el-table-column label="总数量" align="center" prop="znum" />
      <el-table-column label="总金额" align="center" prop="zprice" />
      <el-table-column label="类型" align="center" prop="isCt">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isCt === '1'">采购单</el-tag>
          <el-tag  v-if="scope.row.isCt === '2'">采退单</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
     <el-table-column label="是否入库" align="center" prop="isRk">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isRk === '0'">未入库</el-tag>
          <el-tag type="success" v-if="scope.row.isRk === '1'">入库</el-tag>
          <el-tag type="danger" v-if="scope.row.isRk === '2'">未出库</el-tag>
          <el-tag type="success" v-if="scope.row.isRk === '3'">出库</el-tag>
          <el-tag type="warning" v-if="scope.row.isRk === '4'">未全入库</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isCt === '1'"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supermarket:xqpurchase:list']"
            >入库</el-button
          >

          <el-button
            v-else
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="outStore(scope.row)"
            v-hasPermi="['supermarket:purchase:outStore']"
            >出库</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <el-drawer
      title="采购详情"
      :visible.sync="drawer"
      size="80%"
      @close="handleDrawerClose"
    >
      <el-table
        v-loading="loading"
        :data="xqpurchaseList"
      >
        <el-table-column type="index" label="序号" align="center" />
        <el-table-column label="商品码" align="center" prop="productcode" />
        <el-table-column label="商品名称" align="center" prop="productName" />
        <el-table-column label="规格" align="center" prop="unit" />
        <el-table-column label="数量" align="center" prop="sum" />
        <el-table-column label="价格" align="center" prop="price" />
        <el-table-column label="总金额" align="center" prop="zprice" />
        <el-table-column
          label="生产日期"
          align="center"
          prop="manufactureDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.manufactureDate, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="过期日期"
          align="center"
          prop="expirationDate"
          width="180"
        >
          <template slot-scope="scope">
            <span>{{
              parseTime(scope.row.expirationDate, "{y}-{m}-{d}")
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="品牌" align="center" prop="productBrand" />
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-popover
              ref="popoverRef"
              placement="right"
              width="400"
              trigger="click"
              v-if="scope.row.isRk === '0'"
            >
              <el-form ref="form" :model="form" :rules="rules">
                <el-form-item label="货位">
                  <treeselect
                    v-model="form.whId"
                    :options="warehouseOptions"
                    :normalizer="normalizer"
                    placeholder="请选择货位"
                  />
                </el-form-item>
                <el-button type="primary" @click="rk(scope.row)"
                  >确 定</el-button
                >
              </el-form>
              <el-button
                slot="reference"
                size="mini"
                type="text"
                icon="el-icon-edit"
                v-hasPermi="['supermarket:purchase:rk']"
                >入库</el-button
              >
            </el-popover>

            <span v-else>已入库</span>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>
  </div>
</template>

<script>
import {
  listPurchase,
  getPurchase,
  delPurchase,
  addPurchase,
  updatePurchase,
  ark,
  purchaseOutStore,
} from "@/api/supermarket/purchase";
import {
  listXqpurchase,
  getXqpurchase,
  delXqpurchase,
  addXqpurchase,
  updateXqpurchase,
} from "@/api/supermarket/xqpurchase";
import {
  listWarehouse,
  getWarehouse,
  delWarehouse,
  addWarehouse,
  updateWarehouse,
} from "@/api/supermarket/warehouse";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Purchase",
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      drawer: false,
      // 采购采退汇总表格数据
      purchaseList: [],
      xqpurchaseList: [],
      //货位列表
      warehouseOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        purchaseCode: null,
        supplyId: null,
        znum: null,
        zprice: null,
        createName: null,
        isCt: null,
        isZf: null,
        isRk: null,
        isSh: 0,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    rk(row) {
      this.form.pId = row.pId;
      this.form.purchaseId = row.purchaseId;
      ark(this.form).then((res) => {
        this.$modal.msgSuccess("入库成功");
        this.form.whId = null;
        this.getListXq(row.purchaseId);
      });
    },
    gethwList() {
      listWarehouse(this.queryParams).then((response) => {
        this.warehouseOptions = this.handleTree(response.rows, "whId");
      });
    },
    handleDrawerClose() {
      this.getList();
    },
    /** 转换分类数据结构 */
    normalizer(node) {
      if (node.children && !node.children.length) {
        delete node.children;
      }
      return {
        id: node.whId,
        label: node.whName,
        children: node.children,
      };
    },
    /** 查询采购采退汇总列表 */
    getList() {
      this.loading = true;
      listPurchase(this.queryParams).then((response) => {
        this.purchaseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    getListXq(purchaseId) {
      this.queryParams.purchaseId = purchaseId;
      listXqpurchase(this.queryParams).then((response) => {
        this.xqpurchaseList = response.data;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        purchaseId: null,
        purchaseCode: null,
        supplyId: null,
        znum: null,
        zprice: null,
        createName: null,
        createTime: null,
        isCt: null,
        isZf: null,
        isRk: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // // 多选框选中数据
    // handleSelectionChange(selection) {
    //   this.ids = selection.map((item) => item.purchaseId);
    //   this.single = selection.length !== 1;
    //   this.multiple = !selection.length;
    // },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加采购采退汇总";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.drawer = true;
      this.getListXq(row.purchaseId);
      this.gethwList();
      // this.reset();
      // const purchaseId = row.purchaseId || this.ids
      // getPurchase(purchaseId).then(response => {
      //   this.form = response.data;
      //   this.open = true;
      //   this.title = "修改采购采退汇总";
      // });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.purchaseId != null) {
            updatePurchase(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addPurchase(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const purchaseIds = row.purchaseId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除采购采退汇总编号为"' + purchaseIds + '"的数据项？'
        )
        .then(function () {
          return delPurchase(purchaseIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    storage(row) {},
    //出库操作
    outStore(row) {
      purchaseOutStore(row.purchaseId).then((res) => {
        this.$modal.msgSuccess("出库成功");
        this.getList();
      });
    },
  },
};
</script>
